﻿using QLKetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Linq;
using QLDVu.BO;

namespace QLDVu.GUI
{
    public partial class QuanLyPhieuBaoGia : Form
    {
        int index = 0;
        public static string machitiet;
        public static string madichvu;
        public string maphieubaogia = "";
        BindingList<ChiTietPhieuBaoGia> dschitiet = new BindingList<ChiTietPhieuBaoGia>();
        bool ktthem = false;
        
        public static string mabaogiain;
        public static string makhachhangin;
        public static string biensoxein;
        public QuanLyPhieuBaoGia()
        {
            InitializeComponent();
        }

         public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }

        private void gvChiTiet_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {


                DataGridViewRow row = gvChiTiet.Rows[e.RowIndex];
                if (!row.IsNewRow)
                    gvChiTiet.Rows.Remove(row);
            }
            int tongtien = 0;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                {
                    tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                }
            }
            txtTongTien.Text = tongtien.ToString();
        }

        private void gvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            index = gvChiTiet.CurrentCell.RowIndex;
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                // first remove event handler to keep from attaching multiple:
                cb.SelectedIndexChanged -= new
                EventHandler(LoaiBaoGia_SelectedIndexChanged);

                // now attach the event handler
                cb.SelectedIndexChanged += new
                EventHandler(LoaiBaoGia_SelectedIndexChanged);
            }

            if (gvChiTiet.CurrentCell.ColumnIndex == 3)
            {
                TextBox tbSoLuong = (TextBox)e.Control;
                tbSoLuong.TextChanged += new EventHandler(tbSoLuong_TextChanged);
                tbSoLuong.KeyPress += tbSoLuong_KeyPress;

            }
            if (gvChiTiet.CurrentCell.ColumnIndex == 4)
            {
                TextBox tbDonGia = (TextBox)e.Control;
                tbDonGia.TextChanged += tbDonGia_TextChanged;
                tbDonGia.KeyPress += tbDonGia_KeyPress;
            }
        }
        void tbDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        void tbSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void LoaiBaoGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmbprocess = (ComboBox)sender;
            if (cmbprocess.Text != "" && cmbprocess.Text != null)
            {
                if (cmbprocess.Text.Equals("Dịch Vụ"))
                {
                    DanhSachDichVu frm = new DanhSachDichVu();
                    frm.Button_Clicked += new EventHandler(frm_btnDichVu_Clicked);
                    frm.Show();
                    cmbprocess.SelectedIndex = 1;
                }
                else
                {


                    DanhSachHangHoa frm = new DanhSachHangHoa();
                    frm.Button_Clicked += new EventHandler(frm_btnHangHoa_Clicked);
                    frm.Show();
                    cmbprocess.SelectedIndex = 0;
                }
                /// Your Code goes here
            }
        }

        private void tbDonGia_TextChanged(object sender, EventArgs e)
        {
            index = gvChiTiet.CurrentCell.RowIndex;
            int indecolum = gvChiTiet.CurrentCell.ColumnIndex;
            if (indecolum == 3)
            {
                TextBox soluong = (TextBox)sender;
                if (soluong.Text != "" && gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(soluong.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim());
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }


            }
            else if (indecolum == 4)
            {

                TextBox dongia = (TextBox)sender;
                if (dongia.Text != "" && gvChiTiet.Rows[index].Cells["SoLuong"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(dongia.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["SoLuong"].Value);
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }

            }
            int tongtien = 0;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                {
                    tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                }
            }
            txtTongTien.Text = tongtien.ToString();
        }

        private void tbSoLuong_TextChanged(object sender, EventArgs e)
        {
            index = gvChiTiet.CurrentCell.RowIndex;
            int indecolum = gvChiTiet.CurrentCell.ColumnIndex;
            if (indecolum == 3)
            {
                TextBox soluong = (TextBox)sender;
                if (soluong.Text != "" && gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(soluong.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim());
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }


            }
            else if (indecolum == 4)
            {

                TextBox dongia = (TextBox)sender;
                if (dongia.Text != "" && gvChiTiet.Rows[index].Cells["SoLuong"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(dongia.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["SoLuong"].Value);
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }

            }
            int tongtien = 0;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                {
                    tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                }
            }
            txtTongTien.Text = tongtien.ToString();

        }

        private void frm_btnHangHoa_Clicked(object sender, EventArgs e)
        {
            this.DuLieuHangHoa(machitiet);
        }
        void DuLieuHangHoa(string mahanghoa)
        {
            if (mahanghoa != "")
            {

                HANGHOA hanghoa = BO.DanhSachHangHoaBO.DanhSachHangHoa(mahanghoa);
                //bool kt = false;
                //int tongtien = 0;
                //if (kt == false)
                //{
                //    //string tenhanghoa = cbHangHoa.Text.Trim();
                //    //string mahanghoa = mahanghoa;

                //    dschitiet.Add(new ChiTietPhieuBaoGia(mahanghoa, hanghoa.TenHangHoa,
                //    Convert.ToInt32(txtSoLuong.Text.Trim()), Convert.ToInt32(txtDonGia.Text.Trim()),
                //    Convert.ToInt32(txtThanhTien.Text.Trim())));
                //}
                //foreach (ChiTietPhieuBaoGia chitiet in dschitiet)
                //{

                //    tongtien = tongtien + chitiet.ThanhTien;
                //}

                //txtTongTien.Text = tongtien.ToString();
                string dongia = Convert.ToInt32(ChiTietPhieuBaoGiaBO.DonGiaHangHoa(mahanghoa)).ToString();
                //txtDonGia.Text = dongia;
                DataGridViewRow row = gvChiTiet.Rows[index];

                row.Cells["ChiTietBaoGia"].Value = hanghoa.TenHangHoa;
                gvChiTiet.Rows[index].Cells["DonGia"].Value = dongia;
                gvChiTiet.Rows[index].Cells["SoLuong"].Value = "1";
                gvChiTiet.Rows[index].Cells["ThanhTien"].Value = dongia;
                gvChiTiet.Rows[index].Cells["LoaiBaoGia"].Value = "Hàng Hóa";
                gvChiTiet.Rows[index].Cells["MaChiTietBaoGia"].Value = hanghoa.MaHangHoa;
                gvChiTiet.Rows[index].Cells["Xoa"].Value = "Xóa Chi Tiết";
                int tongtien = 0;
                for (int i = 0; i < gvChiTiet.RowCount; i++)
                {
                    if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                    {
                        tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                    }
                }
                txtTongTien.Text = tongtien.ToString();
                gvChiTiet.CurrentCell = gvChiTiet.Rows[index].Cells["SoLuong"];
                gvChiTiet.BeginEdit(true);
                //gvChiTiet.Rows.Add(row);


            }
        }
        private void frm_btnDichVu_Clicked(object sender, EventArgs e)
        {
            this.DuLieuDichVu(madichvu);
        }
        void DuLieuDichVu(string madichvu)
        {
            if (madichvu != "")
            {

                DICHVU dichvu = BO.DanhSachDichVuBO.DanhSachDichVu(madichvu);
                //bool kt = false;
                //int tongtien = 0;
                //if (kt == false)
                //{
                //    //string tenhanghoa = cbHangHoa.Text.Trim();
                //    //string mahanghoa = mahanghoa;

                //    dschitiet.Add(new ChiTietPhieuBaoGia(mahanghoa, hanghoa.TenHangHoa,
                //    Convert.ToInt32(txtSoLuong.Text.Trim()), Convert.ToInt32(txtDonGia.Text.Trim()),
                //    Convert.ToInt32(txtThanhTien.Text.Trim())));
                //}
                //foreach (ChiTietPhieuBaoGia chitiet in dschitiet)
                //{

                //    tongtien = tongtien + chitiet.ThanhTien;
                //}

                //txtTongTien.Text = tongtien.ToString();
                string dongia = Convert.ToInt32(ChiTietPhieuBaoGiaBO.DanhSachDichVu(madichvu).TienCong).ToString();
                //txtDonGia.Text = dongia;
                DataGridViewRow row = gvChiTiet.Rows[index];

                row.Cells["ChiTietBaoGia"].Value = dichvu.TenDichVu;
                gvChiTiet.Rows[index].Cells["DonGia"].Value = dongia;
                gvChiTiet.Rows[index].Cells["SoLuong"].Value = "1";
                gvChiTiet.Rows[index].Cells["SoLuong"].ReadOnly = true;
                gvChiTiet.Rows[index].Cells["ThanhTien"].Value = dongia;
                gvChiTiet.Rows[index].Cells["LoaiBaoGia"].Value = "Dịch Vụ";
                gvChiTiet.Rows[index].Cells["MaChiTietBaoGia"].Value = dichvu.MaDichVu;
                gvChiTiet.Rows[index].Cells["Xoa"].Value = "Xóa Chi Tiết";
                int tongtien = 0;
                for (int i = 0; i < gvChiTiet.RowCount; i++)
                {
                    if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                    {
                        tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                    }
                }
                txtTongTien.Text = tongtien.ToString();
                gvChiTiet.CurrentCell = gvChiTiet.Rows[index].Cells["DonGia"];
                gvChiTiet.BeginEdit(true);
                //gvChiTiet.Rows.Add(row);


            }
        }

        void LoadDuLieu(string mabaogia)
        {




            gvChiTiet.Rows.Clear();

            if (mabaogia != "")
            {
               

                dschitiet.Clear();
                IEnumerable<CHITIETBAOGIA> chitietbaogia = BO.ChiTietPhieuBaoGiaBO.LayChiTietPhieuBaoGia(maphieubaogia);
                List<CHITIETBAOGIA> listchitiet = chitietbaogia.ToList();
                for (int i = 0; i < listchitiet.Count(); i++)
                {
                    string machitietbaogia = null;
                    string tenchitietbaogia = null;
                    string loaibaogia=null;
                    if (listchitiet[i].MaHangHoa != null)
                    {
                        loaibaogia="Hàng Hóa";
                        machitietbaogia = listchitiet[i].MaHangHoa;
                        tenchitietbaogia = listchitiet[i].HANGHOA.TenHangHoa;
                    }
                    else
                    {
                        loaibaogia="Dịch Vụ";
                        machitietbaogia = listchitiet[i].MaDichVu;
                        tenchitietbaogia = listchitiet[i].DICHVU.TenDichVu;
                    }

                    dschitiet.Add(new ChiTietPhieuBaoGia(loaibaogia,machitietbaogia, tenchitietbaogia, Convert.ToInt32(listchitiet[i].SoLuong), Convert.ToInt32(listchitiet[i].DonGia), Convert.ToInt32(listchitiet[i].ThanhTien)));
                }
                for (int i = 0; i < dschitiet.Count;i++ )
                {
                    gvChiTiet.Rows.Add(dschitiet[i].LoaiBaoGia, dschitiet[i].MaChiTietBaoGia, dschitiet[i].TenChiTietBaoGia, dschitiet[i].SoLuong, dschitiet[i].DonGia, dschitiet[i].ThanhTien,"Xóa chi tiết");
                }
                    

                BAOGIA baogia = BO.ChiTietPhieuBaoGiaBO.PhieuBaoGiaTheoMa(maphieubaogia);
                cbNhanVien.Text = baogia.NGUOIDUNG.TenNguoiDung;
                cbKhachHang.Text = baogia.KHACHHANG.TenKhachHang;
                cbBienSo.Text = baogia.XE.BienSoXe;
                txtTongTien.Text =Convert.ToInt32(baogia.TongTien).ToString();
                txtSoBaoGia.Text = baogia.MaBaoGia;
                dpNgayLapBaoGia.Value = Convert.ToDateTime(baogia.NgayLap);
            }
        }
        public void LoadKhachHang()
        {
            var dskhachhang = from kh in BO.DanhSachKhachHangBO.DanhSachKhachHang()
                              select new { kh.MaKhachHang, kh.TenKhachHang, kh.LOAIKHACHHANG.TenLoaiKhachHang, kh.DienThoai, kh.DiaChi, kh.Email, kh.KHUVUC.TenKhuVuc };

            cbKhachHang.DataSource = dskhachhang;
            cbKhachHang.DisplayMember = "TenKhachHang";
            cbKhachHang.ValueMember = "MaKhachHang";
            //cbKhachHang.DropDownColumns = "TenKhachHang,TenLoaiKhachHang,DienThoai";
            //cbKhachHang.DropDownColumnsHeaders = "Tên Khách Hàng\r\nLoại Khách hàng\r\nĐiện Thoại";
            //cbKhachHang.DropDownWidth = 450;
        }
        public void LoadXe()
        {
            var dsxe = from xe in BO.DanhSachXeBO.DanhSachXe()
                       select new { xe.BienSoXe, xe.HANGSX.TenHangSanXuat, xe.DONGXE.TenDongXe, xe.DOIXE.TenDoiXe };
            cbBienSo.DataSource = dsxe;
            cbBienSo.DisplayMember = "BienSoXe";
            cbBienSo.ValueMember = "BienSoXe";
            //cbBienSo.DropDownColumns = "BienSoXe,TenHangSanXuat,TenDongXe,TenDoiXe";
            //cbBienSo.DropDownColumnsHeaders = "Biển Số\r\nHãng SX\r\nDòng Xe\r\nĐời Xe";
            //cbBienSo.DropDownWidth = 450;
        }
        private void QuanLyPhieuBaoGia_Load(object sender, EventArgs e)
        {
            

            var dsxe = from xe in BO.DanhSachXeBO.DanhSachXe()
                       select new { xe.BienSoXe, xe.HANGSX.TenHangSanXuat, xe.DONGXE.TenDongXe, xe.DOIXE.TenDoiXe };
            cbBienSo.DataSource = dsxe;
            cbBienSo.DisplayMember = "BienSoXe";
            cbBienSo.ValueMember = "BienSoXe";
            //cbBienSo.DropDownColumns = "BienSoXe,TenHangSanXuat,TenDongXe,TenDoiXe";
            //cbBienSo.DropDownColumnsHeaders = "Biển Số\r\nHãng SX\r\nDòng Xe\r\nĐời Xe";
            //cbBienSo.DropDownWidth = 450;
            var dskhachhang = from kh in BO.DanhSachKhachHangBO.DanhSachKhachHang()
                              select new { kh.MaKhachHang, kh.TenKhachHang, kh.LOAIKHACHHANG.TenLoaiKhachHang, kh.DienThoai, kh.DiaChi, kh.Email, kh.KHUVUC.TenKhuVuc };

            cbKhachHang.DataSource = dskhachhang;
            cbKhachHang.DisplayMember = "TenKhachHang";
            cbKhachHang.ValueMember = "MaKhachHang";
            //cbKhachHang.DropDownColumns = "TenKhachHang,TenLoaiKhachHang,DienThoai";
            //cbKhachHang.DropDownColumnsHeaders = "Tên Khách Hàng\r\nLoại Khách hàng\r\nĐiện Thoại";
            //cbKhachHang.DropDownWidth = 450;

            cbNhanVien.DataSource = BO.DanhSachNhanVienBO.DanhSachNhanVien();
            cbNhanVien.DisplayMember = "TenNguoiDung";
            cbNhanVien.ValueMember = "MaNguoiDung";
            //cbNhanVien.DropDownColumns = "MaNguoiDung,TenNguoiDung";
            //cbNhanVien.DropDownColumnsHeaders = "Mã Nhân Viên\r\nTên Nhân Viên\r\n";
            //cbNhanVien.DropDownWidth = 200;

            dpNgayLapBaoGia.CustomFormat = "dd/MM/yyyy";
            dpNgayLapBaoGia.Format = DateTimePickerFormat.Custom;
            txtTongTien.Text = "0";





            Table<BAOGIA> phieubaogia = BO.ChiTietPhieuBaoGiaBO.DanhSachPhieuBaoGia();
            var dsphieubaogia = from ds in phieubaogia
                                select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
            gvPhieuBaoGia.DataSource = dsphieubaogia;
            LoadDuLieu(maphieubaogia);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            ktthem = false;
            cbBienSo.Enabled = true;
            cbKhachHang.Enabled = true;
            cbNhanVien.Enabled = true;
            gvChiTiet.Enabled = true;
            dpNgayLapBaoGia.Enabled = true;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThemKhachHang.Enabled = true;
            btnThemBienSo.Enabled = true;
            btnThem.Enabled = false;
           
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (ktthem == false)
            {
                bool kiemtra = false;


                if (kiemtra == false)
                {
                    ChiTietPhieuBaoGiaBO.CapNhatPhieuBaoGia(txtSoBaoGia.Text.Trim(), cbKhachHang.SelectedValue.ToString(),
                        cbBienSo.SelectedValue.ToString(), cbNhanVien.SelectedValue.ToString(), dpNgayLapBaoGia.Value.ToShortDateString(),
                        txtTongTien.Text.Trim());
                    //ChiTietPhieuBaoGiaBO.ThemPhieuBaoGia(txtMaBaoGia.Text.Trim(), txtMaKhach.Text.Trim(),
                    //    txtBienSo.Text.Trim(), txtMaNhanVien.Text.Trim(), dpNgayLap.Value.ToShortDateString(),
                    //    txtTongTien.Text.Trim());
                    ChiTietPhieuBaoGiaBO.XoaChiTietPhieuBaoGia(txtSoBaoGia.Text.Trim());
                    for (int i = 0; i < gvChiTiet.RowCount; i++)
                    {
                        string machitietbaogia;
                        if (ChiTietPhieuBaoGiaBO.MaChiTietBaoGiaCuoiCung() != null)
                            machitietbaogia = NextID(ChiTietPhieuBaoGiaBO.MaChiTietBaoGiaCuoiCung().MaChiTietBaoGia, "CTBG");
                        else
                            machitietbaogia = "CTBG0001";
                        string madichvu = null;
                        string mahanghoa = null;
                        if (gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value != null)
                        {
                            if (gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim().Substring(0, 2).Equals("DV"))
                            {
                                madichvu = gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim();
                            }
                            else
                            {
                                mahanghoa = gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim();
                            }
                            string dongia = gvChiTiet.Rows[i].Cells["DonGia"].Value.ToString().Trim();
                            string soluong = gvChiTiet.Rows[i].Cells["SoLuong"].Value.ToString().Trim();
                            string thanhtien = gvChiTiet.Rows[i].Cells["ThanhTien"].Value.ToString().Trim();
                            ChiTietPhieuBaoGiaBO.ThemChiTietPhieuBaoGia(machitietbaogia, txtSoBaoGia.Text.Trim(), madichvu, mahanghoa, dongia, soluong, thanhtien);


                        }





                    }
                    Table<BAOGIA> phieubaogia = BO.ChiTietPhieuBaoGiaBO.DanhSachPhieuBaoGia();
                    var dsphieubaogia = from ds in phieubaogia
                                        select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
                    gvPhieuBaoGia.DataSource = dsphieubaogia;
                    MessageBox.Show("Bạn đã lưu thành công");
                }
              
            }
            else
            {
                bool kiemtra = false;
                if (Convert.ToInt32(txtTongTien.Text) == 0)
                {
                    MessageBox.Show("Bạn phải nhập chi tiết hàng hóa");
                    kiemtra = true;
                }

                if (kiemtra == false)
                {
                    string mabaogia2 = "";
                    if (ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung() != null)
                        mabaogia2 = NextID(ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung().MaBaoGia, "BG");
                    else mabaogia2 = "BG0001";
                    ChiTietPhieuBaoGiaBO.ThemPhieuBaoGia(mabaogia2, cbKhachHang.SelectedValue.ToString(),
                        cbBienSo.SelectedValue.ToString(), cbNhanVien.SelectedValue.ToString(), dpNgayLapBaoGia.Value.ToShortDateString(),
                        txtTongTien.Text.Trim());
                    string mabaogia = ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung().MaBaoGia;
                    for (int i = 0; i < gvChiTiet.RowCount; i++)
                    {
                        string machitietbaogia;
                        if (ChiTietPhieuBaoGiaBO.MaChiTietBaoGiaCuoiCung() != null)
                            machitietbaogia = NextID(ChiTietPhieuBaoGiaBO.MaChiTietBaoGiaCuoiCung().MaChiTietBaoGia, "CTBG");
                        else
                            machitietbaogia = "CTBG0001";
                        string madichvu = null;
                        string mahanghoa = null;
                        if (gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value != null)
                        {
                            if (gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim().Substring(0, 2).Equals("DV"))
                            {
                                madichvu = gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim();
                            }
                            else
                            {
                                mahanghoa = gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim();
                            }


                            string dongia = gvChiTiet.Rows[i].Cells["DonGia"].Value.ToString().Trim();
                            string soluong = gvChiTiet.Rows[i].Cells["SoLuong"].Value.ToString().Trim();
                            string thanhtien = gvChiTiet.Rows[i].Cells["ThanhTien"].Value.ToString().Trim();
                            ChiTietPhieuBaoGiaBO.ThemChiTietPhieuBaoGia(machitietbaogia, mabaogia, madichvu, mahanghoa, dongia, soluong, thanhtien);
                        }


                    }


                    Table<BAOGIA> phieubaogia = BO.ChiTietPhieuBaoGiaBO.DanhSachPhieuBaoGia();
                    var dsphieubaogia = from ds in phieubaogia
                                        select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
                    gvPhieuBaoGia.DataSource = dsphieubaogia;
                    MessageBox.Show("Bạn đã thêm vào thành công");
                }
               
            }
            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;
            btnThemKhachHang.Enabled = false;
            btnThemBienSo.Enabled = false;
            cbBienSo.Enabled = false;
            cbKhachHang.Enabled = false;
            cbNhanVien.Enabled = false;
            dpNgayLapBaoGia.Enabled = false;
            gvChiTiet.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult traloi = MessageBox.Show("Bạn có muốn xóa không");
            if (traloi == DialogResult.OK)
            {
                ChiTietPhieuBaoGiaBO.XoaChiTietPhieuBaoGia(txtSoBaoGia.Text.Trim());
                ChiTietPhieuBaoGiaBO.XoaPhieuBaoGia(txtSoBaoGia.Text.Trim());
                MessageBox.Show("Bạn đã xóa thành công");
            }
            Table<BAOGIA> phieubaogia = BO.ChiTietPhieuBaoGiaBO.DanhSachPhieuBaoGia();
            var dsphieubaogia = from ds in phieubaogia
                                select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
            gvPhieuBaoGia.DataSource = dsphieubaogia;
            LoadDuLieu(maphieubaogia);
            
        }

        private void panelEx2_Click(object sender, EventArgs e)
        {

        }

        private void gvPhieuBaoGia_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;

                maphieubaogia = gvPhieuBaoGia.Rows[index].Cells["MaBaoGia"].Value.ToString().Trim();
                LoadDuLieu(maphieubaogia);
                btnLuu.Enabled = false;
                btnSua.Enabled = true;
                btnThem.Enabled = true;
                btnXoa.Enabled = true;
                btnThemKhachHang.Enabled = false;
                btnThemBienSo.Enabled = false;
                cbBienSo.Enabled = false;
                cbKhachHang.Enabled = false;
                cbNhanVien.Enabled = false;
                dpNgayLapBaoGia.Enabled = false;
            }
            catch
            {

            }
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            DanhSachKhachHangForm form = new DanhSachKhachHangForm(this);
            form.Show();
        }

        private void btnThemBienSo_Click(object sender, EventArgs e)
        {
            DanhSachXeForm form = new DanhSachXeForm(this);
            form.Show();
        }

        private void txtBienSo_TextChanged(object sender, EventArgs e)
        {

            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> baogia = dulieu.GetTable<BAOGIA>();
            if(txtBienSoTK.Text.Trim()!="")
            {
                var dsphieubaogia = from ds in baogia
                                    where ds.BienSoXe.Equals(txtBienSoTK.Text.Trim())
                                    select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
                gvPhieuBaoGia.DataSource = dsphieubaogia;
            }
            else
            {
                Table<BAOGIA> phieubaogia = BO.ChiTietPhieuBaoGiaBO.DanhSachPhieuBaoGia();
                var dsphieubaogia = from ds in phieubaogia
                                    select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
                gvPhieuBaoGia.DataSource = dsphieubaogia;
                
            }
            
            
        }

        private void txtBienSoTK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                QLDNDataContext dulieu = new QLDNDataContext();
                Table<BAOGIA> baogia = dulieu.GetTable<BAOGIA>();
              
                var dsphieubaogia = from ds in  baogia
                                    where ds.BienSoXe.Equals(txtBienSoTK.Text.Trim())
                                    select new { ds.MaBaoGia, ds.BienSoXe, ds.KHACHHANG.TenKhachHang, ds.TongTien };
                gvPhieuBaoGia.DataSource = dsphieubaogia;
            }
        }

        private void btnThucHienSC_Click(object sender, EventArgs e)
        {

            SuaChuaForm frm = new SuaChuaForm(txtSoBaoGia.Text);
            frm.Show();
        }

        private void btnThemKhachHang_Click(object sender, EventArgs e)
        {
            DanhSachKhachHangForm form = new DanhSachKhachHangForm(this);
            form.Show();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ktthem = true;
            cbBienSo.Enabled = true;
            cbKhachHang.Enabled = true;
            cbNhanVien.Enabled = true;
            gvChiTiet.Enabled = true;
            dpNgayLapBaoGia.Enabled = true;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThemKhachHang.Enabled = true;
            btnThemBienSo.Enabled = true;
            btnThem.Enabled = false;
           
            if (ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung() != null)
                txtSoBaoGia.Text = NextID(ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung().MaBaoGia, "BG");
            else txtSoBaoGia.Text = "BG0001";
            txtTongTien.Text = "0";
            
        }

        private void btnIn_Click(object sender, EventArgs e)
        {

            mabaogiain = txtSoBaoGia.Text.Trim();
            makhachhangin = cbKhachHang.SelectedValue.ToString();
            biensoxein = cbBienSo.SelectedValue.ToString();
            InPhieuBaoGiaForm inphieubaogia = new InPhieuBaoGiaForm();
            inphieubaogia.Show();
        }

        private void btnInVatTu_Click(object sender, EventArgs e)
        {
            mabaogiain = txtSoBaoGia.Text.Trim();
            makhachhangin = cbKhachHang.SelectedValue.ToString();
            biensoxein = cbBienSo.SelectedValue.ToString();
            InYeuCauVatTuForm inyeucauvattu = new InYeuCauVatTuForm();
            inyeucauvattu.Show();
        }
    }
}
